
// 新增用户
$(function () {
    $("#save-cmsuser-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $("#addcmsuser-dialog");
        //获取元素
        var usernameE = $("input[name=username]");
        var useraccountE = $("input[name=useraccount]");
        var pwdE = $("input[name=pwd]");
        var emailE = $("input[name=email]");
        var mobileE = $("input[name=mobile]");

        var username = usernameE.val();
        var useraccount = useraccountE.val();
        var pwd = pwdE.val();
        var email = emailE.val();
        var mobile = mobileE.val();

        var submitType = self.attr("data-type"); //编辑的时候动态绑定该属性
        var cmsuser_id = self.attr("data-id"); //编辑的时候动态绑定该属性

        if(!username || !useraccount || !email || !mobile){
            if(submitType != 'update' && !pwd){
                amanalert.alertInfo("有必填项没填写，请填写完整后再提交！");
                return;
            }else {
                amanalert.alertInfo("有必填项没填写，请填写完整后再提交！");
                return;
            }
        }

        var url = '';
        if(submitType == 'update'){
            url = '/updatecmsuser/';
            res_message = '用户信息修改成功！'
        }else {
            url = '/addcmsuser/';
            res_message = '用户新增成功！'
        }
        amanajax.post({
            'url':url,
            'data':{
                'username':username,
                'useraccount':useraccount,
                'password':pwd,
                'email':email,
                'mobile':mobile,
                'cmsuser_id':cmsuser_id
            },
            'success':function (data) {
                if(data['code'] == 1){
                    dialog.modal('hide');
                    amanalert.alertSuccessToast(res_message);
                    // 修改成功后清空输入框
                    usernameE.val("");
                    useraccountE.val("");
                    pwdE.val("");
                    emailE.val("");
                    mobileE.val("");
                    window.location.reload();
                }else {
                    amanalert.alertInfo(data['message']);
                }
            },
            'fail':function (error) {
                // console.log(error);
                amanalert.alertNetworkError()
            }
        });
    });
});

// 编辑
$(function () {
    $(".edit-cmsuser-btn").click(function (event) {
        // $("#addcmsuser-dialog").modal({
        //     backdrop: false, //点击空bai白处不关闭对话框，可以防止出现打开编辑直接关闭后再添加用户时带入信息的问题
        // });
        event.preventDefault();
        var self = $(this);
        var dialog = $("#addcmsuser-dialog");

        var useraccountE = dialog.find("input[name='useraccount']");
        var passwordE = dialog.find("#password-class");
        useraccountE.attr("disabled", "disabled");  // 禁止修改帐号
        passwordE.css("display", "none");  // 隐藏密码

        dialog.modal("show");
        var tr = self.parent().parent();

        var username = tr.attr("data-username");
        var useraccount = tr.attr("data-useraccount");
        var email = tr.attr("data-email");
        var mobile = tr.attr("data-mobile");

        var usernameE = dialog.find("input[name='username']");
        var emailE = dialog.find("input[name='email']");
        var mobileE = dialog.find("input[name='mobile']");
        var saveBtn = dialog.find("#save-cmsuser-btn");

        usernameE.val(username);
        useraccountE.val(useraccount);
        emailE.val(email);
        mobileE.val(mobile);

        saveBtn.attr("data-type", "update"); // 给保存按钮绑定一个data-type属性，值为update
        saveBtn.attr("data-id", tr.attr("data-id"));

    });
});

// 启用/停用
$(function () {
    $(".status-cmsuser-btn").click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var cmsuser_id = tr.attr('data-id');
        var statusText = this.value;
        amanajax.post({
            url: "/updatestatus/",
            data:{
                'cmsuser_id':cmsuser_id,
                'status':statusText
            },
            'success':function (data) {
                if(data['code'] == 1){
                    window.location.reload();
                }else {
                    amanalert.alertConfirm({
                        "msg":data['message'],
                        "confirmCallback":function () {
                            window.location.reload();
                        },
                        "cancelCallback":function () {
                            window.location.reload();
                        }
                    });
                }
            },
            'fail':function (error) {
                // console.log(error);
                amanalert.alertNetworkError();
            }
        });
    });
});

// 重置密码
$(function () {
    $(".resetPwd-btn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var cmsuser_id = tr.attr('data-id');
        amanajax.post({
            'url':'/resetpassword/',
            'data':{
                'cmsuser_id':cmsuser_id
            },
            'success':function (data) {
                console.log('data=',data);
                if(data['code'] == 1){
                    amanalert.alertSuccess(data['message']);
                }else {
                    amanalert.alertError(data['message']);
                }
            },
            'fail':function (error) {
                amanalert.alertNetworkError();
            }
        })
    });
});

// 删除
$(function () {
    $(".delete-cmsuser-btn").click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var cmsuser_id = tr.attr('data-id');
        amanalert.alertConfirm({
            "msg":"确定删除这个用户吗？",
            "confirmCallback":function () {
                amanajax.post({
                    'url':'/delcmsuser/',
                    'data':{
                        'cmsuser_id':cmsuser_id
                    },
                    'success':function (data) {
                        console.log(data['code']);
                        if(data['code'] == 1){
                            amanalert.alertSuccessToast('删除成功！');
                            window.location.reload();
                        }else {
                            amanalert.alertToast(data['message']);
                        }
                    },
                    'fail':function (error) {
                        amanalert.alertNetworkError();
                    }
                })
            }
        });
    });
});